package org.example.Imp;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.dao.StudentDao;
import org.example.StudentService;
import org.example.domain.Student;
import org.example.pageParam.CommonData;
import org.example.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImp implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public Student selectStudentByMnemonic_code(String mnemonic) {
        return studentDao.selectStudentByMnemonic_code(mnemonic);
    }

    @Override
    public String saves(List<Student> studentList) {
        int successCount = 0;
        int failCount = 0;
        int total = studentList.size();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0;i< studentList.size();i++){
            Student temp = studentList.get(i);
            try {
                studentDao.save(temp);
                successCount+=1;
                stringBuilder.append(temp.getSname()+"保存成功|");
            }catch (Exception e){
                failCount+=1;
                stringBuilder.append(temp.getSname()+"用户名重复,保存失败|");
            }
        }
        return stringBuilder.toString();
    }



    @Override
    public PageVO query(Map<String, Object> map) {
        //这里利用mybatis的分页插件 pageHelper   下面这一行代码 相当于 帮我们做了 参数的处理 并且执行sql时帮我们拼接limit
        PageHelper.startPage((Integer) map.get("page"),(Integer) map.get("count"));
        //还是需要调用dao方法
        List<Student> studentList = studentDao.query(map);
        PageInfo pageInfo = new PageInfo(studentList);
        PageVO pageVO = new PageVO();
        pageVO.setParams(pageInfo.getList());
        pageVO.setFilterName(map.get("grade")+"-"+map.get("major")+"-"+map.get("class_no"));
        pageVO.setPage(pageInfo.getPageNum());
        pageVO.setCount(pageInfo.getPageSize());
        pageVO.setStart(((Integer) map.get("page")-1)*(Integer) map.get("count"));
        pageVO.setLength((Integer) map.get("count"));
        pageVO.setRealPage(pageInfo.getPages());
        pageVO.setTotal((int)pageInfo.getTotal());
        pageVO.setPageStart(pageInfo.getPageNum());
        pageVO.setPageEnd(pageInfo.getPages());
        return pageVO;

    }

    @Override
    public List<Student> queryClassStudent(Map<String, String> map) {
        return studentDao.queryClassStudent(map);
    }

    @Override
    public Boolean addStudent(Student student) {
        Boolean result = null;
        student.setPass(DigestUtil.md5Hex(CommonData.DEFAULT_PASS));
        student.setMnemonic_code(PinyinUtil.getPinyin(student.getSname(),""));
        try{
            studentDao.addOneStudent(student);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    @Override
    public Student editStudentByCode(String code) {
        return studentDao.editStudent(code);
    }

    @Override
    public Boolean editSave(Student student) {
        Boolean result = null;
        try{
            studentDao.editSave(student);
            result = true;
        }catch (Exception e){
            result = false;
        }
        return result;
    }

    @Override
    public void deleteClass(String className) {
        String[] classNames = className.split("\\|");
        for (int i = 0;i<classNames.length;i++){
            Map<String,String> map = new HashMap<String,String>();
            String[] temp = classNames[i].split("-");
            map.put("grade",temp[0]);
            map.put("major",temp[1]);
            map.put("class_no",temp[2]);
            studentDao.deleteClass(map);
        }
    }

    @Override
    public void deleteClassStudent(String studentInfo) {
        boolean result = studentInfo.contains(",");
        if (result){
            String[] students = studentInfo.split(",");
            for (int i =0;i<students.length;i++){
                String[] temp = students[i].split("-");
                Map<String,String> map = new HashMap<>();
                map.put("grade",temp[0]);
                map.put("major",temp[1]);
                map.put("class_no",temp[2]);
                map.put("code",temp[3]);
                studentDao.deleteClassStudent(map);
            }
        }
        String[] temp = studentInfo.split("-");
        Map<String,String> map = new HashMap<>();
        map.put("grade",temp[0]);
        map.put("major",temp[1]);
        map.put("class_no",temp[2]);
        map.put("code",temp[3]);
        studentDao.deleteClassStudent(map);
    }

    @Override
    public String selectStudentCode(String classInfo,String excludeStudentCode) {
        return studentDao.selectStudentAllCode(classInfo,excludeStudentCode);
    }

    @Override
    public int selectClassStudentTotalNumber(String classInfo) {
        return studentDao.selectClassStudentTotalNumber(classInfo);
    }

    @Override
    public List<Student> selectStudentByNameAndCode(List<String> studentsInfo) {
        return studentDao.selectStudentByNameAndCode(studentsInfo);
    }

    @Override
    public int judgeClassExist(String classInfo) {
        return studentDao.judgeClassExist(classInfo);
    }

    @Override
    public List<Student> selectStudentByClassInfo(String classInfo) {
        return studentDao.selectStudentByClassInfo(classInfo);
    }

    @Override
    public List<Student> selectStudentByCodes(List<String> codes) {
        return studentDao.selectStudentByCodes(codes);
    }

    @Override
    public List<Student> selectNoneExistStudentByCodes(String classname,List<String> codes) {
        return studentDao.selectNoneExistStudentByCodes(classname,codes);
    }

    @Override
    public String selectClassAllStudentCode(String classInfo) {
        return studentDao.selectClassAllStudentCode(classInfo);
    }

    @Override
    public String selectClassAllStudentId(String classInfo) {
        return studentDao.selectClassAllStudentId(classInfo);
    }

    @Override
    public String selectIdsByCodes(List<String> codes) {
        return studentDao.selectClassIdsByCodes(codes);
    }

    @Override
    public List<Student> selectStudentByIds(List<Long> ids) {
        return studentDao.selectStudentByIds(ids);
    }
}
